﻿using System;
using ToolUtil;

namespace zymarc
{
    public class GetBooks
    {
        public static Books SetBooks(String marc)
        {
            MarcConvert convert = new MarcConvert();
            try
            {
                convert.ISOData = marc;
                if (marc.Split('\x001e')[0].Length < 25)
                {
                    convert.MarcText = marc;
                    convert.SetISOStr();
                    convert.SetISOtoMarc();
                }
                else
                {
                    convert.SetISOtoMarc();
                }
                string isbn = convert.GetASubField("010", "a", 1);
                string sisbn = string.Empty;
                if (!string.IsNullOrEmpty(isbn))
                    sisbn = StringUtil.BuingISBNSept(StringUtil.BarCodeToISBN(isbn.Replace("-", "")));
                string ydj = convert.GetASubField("010", "d", 1);
                string ztitle = convert.GetASubField("200", "a", 1);
                string title = convert.GetASubField("200", "a", 1);
                string btitle = convert.GetASubField("200", "d", 1);
                string ftitle = convert.GetASubField("200", "e", 1);
                string fjtitle = convert.GetASubField("200", "h", 1);
                string fctitle = convert.GetASubField("200", "i", 1);
                string author = convert.GetASubField("200", "f", 1);
                string fy = convert.GetASubField("200", "g", 1);
                string tpy = convert.GetASubField("200", "A", 1);
                string press = convert.GetASubField("210", "c", 1);
                string pdate = convert.GetASubField("210", "d", 1);
                string ssh = convert.GetASubField("690", "a", 1);
                string yds = convert.GetASubField("905", "f", 1);
                if (!string.IsNullOrEmpty(fy))
                    author += ";" + fy;
                if (!string.IsNullOrEmpty(btitle))
                    title += "=" + btitle;
                if (!string.IsNullOrEmpty(ftitle))
                    title += ":" + ftitle;
                if (!string.IsNullOrEmpty(fjtitle))
                    title += "." + fjtitle;
                if (!string.IsNullOrEmpty(fctitle))
                    title += "," + fctitle;
                if(string.IsNullOrEmpty(tpy))
                    tpy = convert.GetASubField("200", "9", 1);
                if (!string.IsNullOrEmpty(tpy))
                {
                    string[] nrs = tpy.Split(new char[] { ' ' });
                    tpy = "";
                    foreach (string str in nrs)
                    {
                        tpy += str.Substring(0, 1).ToLower();
                    }
                    if (tpy.Length > 16)
                        tpy = tpy.Substring(0, 15);
                } 
                Books book = new Books();
                book.Isbn = isbn;
                book.Sisbn = sisbn;
                book.Title = title;
                book.Ztitle = ztitle;
                book.Author = author;
                book.Clrq = DateTime.Now;
                book.Press = press;
                book.Pdate = pdate;
                book.Marc = convert.MarcText;
                book.Tmsx = tpy;
                book.Ssh = ssh;
                book.Ydj = ydj;
                book.Sdj = StringUtil.GetDj(ydj);
                try { book.Count = Convert.ToInt32(yds); }
                catch { book.Count = 3; }
                return book;
            }
            catch {
                return null;
            }
        }
    }
}
